﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MaxLeaf.Common;
using System.Data.SqlClient;
using MaxLeaf.DB;
using System.Data;


namespace MaxLeaf.DAL.K2K
{
    public class t_AccountDAL : BaseDAL
    {
         /// <summary>
        /// 会计科目
         /// </summary>
        public t_AccountDAL()
            : base()
        {
            strTableName = "t_Account";
            strMapKeyFieldName = "FAccountID";
            mblnNeedEditKeyValue = false;
            strFieldNames = "FNumber,FName,FLevel,FDetail,FParentID,FRootID,FGroupID,FDC,FHelperCode,FCurrencyID,FAdjustRate,FEarnAccountID,FQuantities,FUnitGroupID,FMeasureUnitID,FIsCash,FIsBank,FJournal,FContact,FIsCashFlow,FDetailID,FAcnt,FLoan,FDpst,FStatedDpst,FInterest,FIsAcnt,FIsBudget,FAcntID,FBrNo,FAcctint,FintRate,FLastintDate,FAcntType,FTradeNum,FControl,FViewMsg,FMessage,FDelete,FIsBusi,FFullName,FSystemType,FControlSystem,UUID,FCFItemID,FSubCFItemID,FClassTypeID";
        }

        public override int Update(DBAccess dbOrgK3Con, DBAccess dbDestK3Con, string strKeyValue)
        {
            return 0;
            //不分发
        }

        protected override void DAL_GetFieldData(DBAccess dbOrgK3Con, DBAccess dbDestK3Con, string strFieldName, DataRow drOrgData, DataRow drDestData)
        {
            if (strFieldName.Equals("FParentID", StringComparison.InvariantCultureIgnoreCase))
            {
                string[] arrFnumber = CvtFunc.ToString(drOrgData["Fnumber"]).Split('.');
                if (arrFnumber.Length > 1)
                {
                    drDestData[strFieldName] = GetFParentID(dbOrgK3Con, dbDestK3Con, arrFnumber);
                }
                else
                {
                    drDestData[strFieldName] = "0";
                };
            }
            else
            {
                base.DAL_GetFieldData(dbOrgK3Con, dbDestK3Con, strFieldName, drOrgData, drDestData);
            }
        }

        private string GetFParentID(DBAccess dbOrgK3Con, DBAccess dbDestK3Con, string[] arrFnumber)
        {
            string strFAcctID = string.Empty;
           
            for (int i = 0; i < arrFnumber.Length - 1; i++)
            {
                DataRow drOrg = dbOrgK3Con.GetTableDataFirstRow(strTableName, new Dictionary<string, object> { { "FNumber", string.Join(".", arrFnumber, 0, i + 1) } });
                string strFParentID = "0";
                if (i != 0)
                {
                    strFParentID = strFAcctID;
                }
                //得到每级的FitemID
                t_AccountDAL dal = new t_AccountDAL();
                strFAcctID = dal.GetDestMapKeyValue(dbOrgK3Con, dbDestK3Con, CvtFunc.ToString(drOrg[strMapKeyFieldName]));

                
            }

            return strFAcctID;
        }


        public override string GetDestMapKeyValue(DBAccess dbOrgK3Con, DBAccess dbDestK3Con, string strOrgAcctID)
        {
            DataRow drOrg = dbOrgK3Con.GetTableDataFirstRow(strTableName, new Dictionary<string, object> { { "FAccountID", strOrgAcctID } }, "FNumber");
            if (drOrg == null) return "0";
            string strOrgFNumber = CvtFunc.ToString(drOrg["FNumber"]);
 

            string strDestFNumber =  strOrgFNumber ;
            DataRow drDest = dbDestK3Con.GetTableDataFirstRow(strTableName, new Dictionary<string, object> { { "FNumber", strDestFNumber } }, "FAccountID");
            if (drDest == null) return "0";

            return CvtFunc.ToString(drDest["FAccountID"]);
        }
         
    }
}